﻿using UnityEditor;
using UnityEngine;
namespace Miao
{
    public class SpriteSettingsApplier : EditorWindow
    {
        [MenuItem("Miao/Sprite Edtior中编辑内容批量应用到其他Sprite")]
        public static void ShowWindow()
        {
            GetWindow<SpriteSettingsApplier>("Sprite Settings Applier");
        }

        public Texture2D sourceSprite;
        public Texture2D[] targetSprites;

        void OnGUI()
        {
            sourceSprite = EditorGUILayout.ObjectField("Source Sprite", sourceSprite, typeof(Texture2D), false) as Texture2D;
            ScriptableObject target = this;
            SerializedObject so = new SerializedObject(target);
            SerializedProperty stringsProperty = so.FindProperty("targetSprites");
            EditorGUILayout.PropertyField(stringsProperty, true);
            so.ApplyModifiedProperties();

            if (GUILayout.Button("Apply Settings"))
            {
                if (sourceSprite != null && targetSprites != null && targetSprites.Length > 0)
                {
                    string sourcePath = AssetDatabase.GetAssetPath(sourceSprite);
                    TextureImporter sourceImporter = AssetImporter.GetAtPath(sourcePath) as TextureImporter;

                    foreach (var targetSprite in targetSprites)
                    {
                        string targetPath = AssetDatabase.GetAssetPath(targetSprite);
                        TextureImporter targetImporter = AssetImporter.GetAtPath(targetPath) as TextureImporter;

                        // 复制设置
                        targetImporter.spritePixelsPerUnit = sourceImporter.spritePixelsPerUnit;
                        //targetImporter.spriteMeshType = sourceImporter.spriteMeshType;
                        targetImporter.spritePivot = sourceImporter.spritePivot;
                        targetImporter.spriteBorder = sourceImporter.spriteBorder;

                        EditorUtility.SetDirty(targetImporter);
                        targetImporter.SaveAndReimport();
                    }
                }
            }
        }
    }
}